from peewee import *

# 连接数据库
db = SqliteDatabase('people.db')


# 创建模型
class Person(Model):
    name = CharField()
    birthday = DateField()

    class Meta:
        database = db  # This model uses the "people.db" database.


class Pet(Model):
    owner = ForeignKeyField(Person, backref='pets')
    name = CharField()
    animal_type = CharField()

    class Meta:
        database = db  # this model uses the "people.db" database


# 创建表
db.connect()
db.create_tables([Person, Pet])

# 排序查询
uncle_bob = Person.get(Person.id == 7)
herb_fido = Pet.create(owner=uncle_bob, name='Fido', animal_type='dog')
herb_mittens = Pet.create(owner=uncle_bob, name='Mittens', animal_type='cat')
herb_mittens_jr = Pet.create(owner=uncle_bob, name='Mittens Jr', animal_type='cat')

for pet in Pet.select().where(Pet.owner == uncle_bob).order_by(Pet.name):
    print(pet.name)
